const db = uniCloud.database();
const dbCmd = db.command;
const $ = db.command.aggregate;
const { defaultChpaters, chapterNameSplitStr } = require('../../config/index.js');
module.exports = async function(options = {}) {
	const { characterId } = options;
	const token = this.getUniIdToken();
	if (!token || !characterId) {
		throw new Error('非法访问');
	}
	let userChapter = [];
	let [chapterList, chapterListCustom] = await Promise.all([
		db.collection('uni-id-users')
		.aggregate()
		.match({
			token
		})
		.lookup({
			from: 'a-dz-chapter-list',
			let: {
				mainid: { $ifNull: ['$chapters', defaultChpaters] }
			},
			pipeline: $.pipeline()
				.match(dbCmd.expr($.in(['$chapter', '$$mainid'])))
				.lookup({
					from: 'a-dz-course-list',
					let: {
						mainid: '$chapter'
					},
					pipeline: $.pipeline().match(dbCmd.expr($.eq(['$chapter', '$$mainid']))).done(),
					as: 'courseList'
				})
				.lookup({
					from: 'a-dz-complete-list',
					let: {
						mainid: '$chapter'
					},
					pipeline: $.pipeline().match(dbCmd.expr($.and([
						$.eq(['$chapter', '$$mainid']),
						$.eq(['$characterId', characterId])
					]))).done(),
					as: 'completeList'
				})
				.project({
					_id: 0,
					name: 1,
					chapter: 1,
					chapterOrder: 1,
					chapterDesc: 1,
					logo: 1,
					courseCount: $.size('$courseList'),
					completeCourseCount: $.size('$completeList')
				})
				.done(),
			as: 'chapters'
		})
		.project({
			chapters: 1
		})
		.end(),
		// 获取自定义的课程
		db.collection('uni-id-users')
		.aggregate()
		.match({
			token
		})
		.addFields({
			"chapters_custom_filter": {
				$map: {
					input: { $ifNull: ["$chapters_custom", []] },
					as: "chapter",
					in: {
						$arrayElemAt: [
							{ $split: ["$$chapter", chapterNameSplitStr] },
							1
						]
					}
				}
			}
		})
		.lookup({
			from: 'a-dz-chapter-list-custom',
			let: {
				mainid: { $ifNull: ['$chapters_custom_filter', []] }
			},
			pipeline: $.pipeline()
				.match(dbCmd.expr($.in(['$chapter', '$$mainid'])))
				.lookup({
					from: 'a-dz-course-list-custom',
					let: {
						mainid: '$chapter'
					},
					pipeline: $.pipeline().match(dbCmd.expr($.eq(['$chapter', '$$mainid']))).done(),
					as: 'courseList'
				})
				.lookup({
					from: 'a-dz-complete-list-custom',
					let: {
						mainid: '$chapter'
					},
					pipeline: $.pipeline().match(dbCmd.expr($.and([
						$.eq(['$chapter', '$$mainid']),
						$.eq(['$characterId', characterId])
					]))).done(),
					as: 'completeList'
				})
				.project({
					_id: 0,
					name: 1,
					chapter: 1,
					chapterOrder: 1,
					chapterDesc: 1,
					logo: 1,
					courseCount: $.size('$courseList'),
					completeCourseCount: $.size('$completeList')
				})
				.done(),
			as: 'chapters'
		})
		.project({
			chapters: 1
		})
		.end()
	])
	chapterList = chapterList.data.length && chapterList.data[0] && chapterList.data[0].chapters;
	chapterListCustom = chapterListCustom.data.length && chapterListCustom.data[0] && chapterListCustom.data[0].chapters;
	chapterList = chapterList ? chapterList : [];
	chapterListCustom = chapterListCustom ? chapterListCustom : [];
	return {
		code: 0,
		data: chapterList.concat(chapterListCustom),
		msg: '获取成功'
	}
}